package com.ruoyi.miniapp.controller;


import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.miniapp.api.domain.MiniappDictData;
import com.ruoyi.miniapp.service.IMiniappDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 字典数据Controller
 *
 * @author ruoyi
 * @date 2023-09-16
 */
@RestController
@RequestMapping("/data")
public class MiniappDictDataController extends BaseController
{
    @Autowired
    private IMiniappDictDataService miniappDictDataService;

    /**
     * 查询字典数据列表
     */
    @RequiresPermissions("miniapp:data:list")
    @GetMapping("/list")
    public TableDataInfo list(MiniappDictData miniappDictData)
    {
        startPage();
        List<MiniappDictData> list = miniappDictDataService.selectMiniappDictDataList(miniappDictData);
        return getDataTable(list);
    }

    /**
     * 导出字典数据列表
     */
    @RequiresPermissions("miniapp:data:export")
    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MiniappDictData miniappDictData)
    {
        List<MiniappDictData> list = miniappDictDataService.selectMiniappDictDataList(miniappDictData);
        ExcelUtil<MiniappDictData> util = new ExcelUtil<MiniappDictData>(MiniappDictData.class);
        util.exportExcel(response, list, "字典数据数据");
    }

    /**
     * 获取字典数据详细信息
     */
    @RequiresPermissions("miniapp:data:query")
    @GetMapping(value = "/{dictCode}")
    public AjaxResult getInfo(@PathVariable("dictCode") Long dictCode)
    {
        return success(miniappDictDataService.selectMiniappDictDataByDictCode(dictCode));
    }

    /**
     * 新增字典数据
     */
    @RequiresPermissions("miniapp:data:add")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MiniappDictData miniappDictData)
    {
        return toAjax(miniappDictDataService.insertMiniappDictData(miniappDictData));
    }

    /**
     * 修改字典数据
     */
    @RequiresPermissions("miniapp:data:edit")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MiniappDictData miniappDictData)
    {
        return toAjax(miniappDictDataService.updateMiniappDictData(miniappDictData));
    }

    /**
     * 删除字典数据
     */
    @RequiresPermissions("miniapp:data:remove")
    @Log(title = "字典数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{dictCodes}")
    public AjaxResult remove(@PathVariable Long[] dictCodes)
    {
        return toAjax(miniappDictDataService.deleteMiniappDictDataByDictCodes(dictCodes));
    }
}
